package com.zndroid.gridview.inner.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.res.ResourcesCompat;

import com.zndroid.gridview.R;

/**
 * @author lzy
 */
public class GridDragImageViewWrapper extends AppCompatImageView {
    private Drawable deleteIcon;

    private int iconWidth;
    private int iconHeight;
    private boolean showIcon = true;
    private Rect mDeleteRect;
    private Rect mAssumeDeleteRect;

    private Bitmap bitmap;                //覆盖图层
    private Rect src;
    private Rect dst;

    private IDeleteClick deleteClick;

    public GridDragImageViewWrapper(@NonNull Context context) {
        this(context, null);
    }

    public GridDragImageViewWrapper(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GridDragImageViewWrapper(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init(context);
    }

    private void init(Context context) {
        src = new Rect();
        dst = new Rect();
        deleteIcon = ResourcesCompat.getDrawable(context.getResources(), R.drawable.ic_gdl_default_delete, context.getTheme());
    }

    public void setCoverBitmap(Bitmap coverBitmap) {
        bitmap = coverBitmap;
        invalidate();
    }

    public void setDeleteIcon(Drawable deleteIcon) {
        this.deleteIcon = deleteIcon;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mAssumeDeleteRect == null) {
            setDeleteBounds();
        }

        if (showIcon) {
            deleteIcon.draw(canvas);
        }

        //绘制覆盖图层
        if (null != bitmap) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();

            src.set(0, 0, width, height);

            //充满
            dst.set(0, 0, getWidth(), getHeight());
            //居中1/4处
//            dst.set(3 * getWidth()/4 - width/2, 3 * getHeight()/4 - height/2, 3 * getWidth()/4, 3 * getHeight()/4);

            canvas.drawBitmap(bitmap, src, dst, null);
            canvas.save();
        }
    }

    public void showDeleteIcon(boolean show) {
        showIcon = show;
        invalidate();
    }

    private void setDeleteBounds() {
        iconWidth = deleteIcon.getIntrinsicWidth();
        iconHeight = deleteIcon.getIntrinsicHeight();
        int left = getWidth() - iconWidth - 4;
        int top = 4;
        mDeleteRect = new Rect(left, top, left + iconWidth, top + iconHeight);
        //padding扩大了icon的点击范围
        int padding = dip2px(getContext(), 10);
        mAssumeDeleteRect = new Rect(mDeleteRect.left, mDeleteRect.top, mDeleteRect.left + iconWidth + padding, mDeleteRect.top + iconHeight + padding);
        deleteIcon.setBounds(mDeleteRect);
    }

    private int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (dpValue * scale + 0.5f);
    }

    public interface IDeleteClick {
        /**
         * item delete
         * @param view currentDeleteView
         * */
        void onItemDeleteClicked(View view);
    }

    public void setDeleteClick(IDeleteClick deleteClick) {
        this.deleteClick = deleteClick;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        int x = (int) event.getX();
        int y = (int) event.getY();
        boolean contains = mAssumeDeleteRect.contains(x, y);
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                if (contains && showIcon) {
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
                if (contains && showIcon) {
                    if (deleteClick != null) {
                        deleteClick.onItemDeleteClicked(this);
                    }
                    return true;
                }
                break;
            case MotionEvent.ACTION_MOVE:
            default:
                break;
        }
        return super.onTouchEvent(event);
    }
}
